use std::convert::From;
use yew::prelude::*;
use yew::services::fetch::{FetchTask};
use yew::services::FetchService;
use yew::html::Component;
use serde_derive::{Serialize};
use serde::{ser, de};
//use std::fmt::Debug;
use super::FetchMsg;
use crate::types::SwapData;
//use crate::web::LocalStorage;
//use crate::json::JsonPager;
//use chrono::{Local};


pub struct Api;

#[derive(Serialize)]
pub struct ApiCached<T: ser::Serialize + de::DeserializeOwned> {
    created: i64, //缓存时间
    data: T,
}

impl Api {

    pub fn send<T: Component>(url: &'static str, link: &ComponentLink<T>) -> FetchTask
        where <T as Component>::Message: From<FetchMsg<SwapData>>
    {
        let callback = link.callback(fetch_callback!());
        let request = request_post!(&url);
        //let mut fetch_service = FetchService::new();
        //fetch_service.fetch(request, callback).unwrap()
        FetchService::fetch(request, callback).unwrap()
    }

    // pub fn get_cached<T: ser::Serialize + de::DeserializeOwned>(key: &'static str) -> Option<JsonPager<T>> {
    //     if let Some(v) = LocalStorage::get::<ApiCached<T>>(key) {
    //         let current_time = Local::now().timestamp();
    //         if current_time - v.created < 10 {
    //             return Some(v.data);
    //         }
    //     }

    //     None
    // }
}